package com.zhima.controller.plain;

import com.zhima.dto.ChangePasswordForm;
import com.zhima.dto.ZhimaOjResult;
import com.zhima.service.OjUserService;
import com.zhima.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.zhima.dto.ZhimaOjResult.success;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhima
 * @since 2023-08-23
 */
@RestController
@RequestMapping("/user")
public class OjUserController {
    @Autowired
    private OjUserService ojUserService;

    /**
     * 根据{userId}获取基本用户信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/basic/{userId}")
    public ZhimaOjResult basicInfoById(@PathVariable("userId") Long userId) {
        return success(ojUserService.getUserCenterInfo(userId));
    }

    /**
     * 修改用户名
     *
     * @param userName
     * @return
     */
    @PostMapping("/user-name/{userName}")
    public ZhimaOjResult changeUserName(@PathVariable("userName") String userName) {
        return success(ojUserService.changeUserName(UserHolder.get().getId(), userName));
    }

    /**
     * 修改手机号
     *
     * @param phone
     * @return
     */
    @PostMapping("/phone/{phone}/{checkCode}")
    public ZhimaOjResult changePhone(@PathVariable("phone") String phone, @PathVariable("checkCode") String checkCode) {
        return success(ojUserService.changePhone(UserHolder.get().getId(), phone, checkCode));
    }

    /**
     * 修改邮箱
     *
     * @param email
     * @return
     */
    @PostMapping("/email/{email}/{checkCode}")
    public ZhimaOjResult changeEmail(@PathVariable("email") String email, @PathVariable("checkCode") String checkCode) {
        return success(ojUserService.changeEmail(UserHolder.get().getId(), email, checkCode));
    }

    /**
     * 修改密码
     *
     * @param changePasswordForm
     * @return
     */
    @PostMapping("/password")
    public ZhimaOjResult changePassword(@RequestBody ChangePasswordForm changePasswordForm) {
        return success(ojUserService.changePassword(UserHolder.get().getId(), changePasswordForm));
    }

    /**
     * 首页的过题数AC榜单
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/ac-board/{pageNum}/{pageSize}")
    public ZhimaOjResult getAcBoard(@PathVariable("pageNum") Long pageNum, @PathVariable("pageSize") Long pageSize) {
        return success(ojUserService.getAcBoard(UserHolder.get().getId(), pageNum, pageSize));
    }
}
